//
//  GQHPasswordSettingView.m
//  Xanthium
//
//  Created by GuanQinghao on 24/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHPasswordSettingView.h"
#import "GQHHeader.h"

@interface GQHPasswordSettingView () <UITextFieldDelegate> {
    
    UITextField *passwordTextField;
    UITextField *againTextField;
}

@end

@implementation GQHPasswordSettingView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kClearColor;
        [self loadUserInterface];
    }
    return self;
}

- (void)loadUserInterface {
    
    UIView *containerView = [[UIView alloc] init];
    containerView.layer.cornerRadius = 5.0;
    containerView.layer.masksToBounds = YES;
    containerView.backgroundColor = kWhiteColor;
    [self addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.inset(20);
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight + 30);
        make.height.mas_equalTo(122);
    }];
    
    UIImageView *passwordImageView = [[UIImageView alloc] init];
    passwordImageView.image = [UIImage imageNamed:@"login_password"];
    [containerView addSubview:passwordImageView];
    [passwordImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.top.and.width.and.height.mas_equalTo(20);
    }];
    
    passwordTextField = [[UITextField alloc] init];
    passwordTextField.placeholder = @"请设置6-12位字母数字组合密码";
    passwordTextField.secureTextEntry = YES;
    [containerView addSubview:passwordTextField];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(passwordImageView.mas_right).with.offset(20);
        make.right.mas_equalTo(containerView).with.inset(20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(passwordImageView);
    }];
    
    UIView *splitView = [[UIView alloc] init];
    splitView.layer.cornerRadius = 1.0;
    splitView.layer.masksToBounds = YES;
    splitView.backgroundColor = kLightGrayColor;
    [containerView addSubview:splitView];
    [splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(containerView).with.offset(20);
        make.right.mas_equalTo(containerView).with.inset(20);
        make.center.mas_equalTo(containerView);
        make.height.mas_equalTo(2);
    }];
    
    UIImageView *againImageView = [[UIImageView alloc] init];
    againImageView.image = [UIImage imageNamed:@"login_password"];
    [containerView addSubview:againImageView];
    [againImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.width.and.height.mas_equalTo(20);
        make.top.mas_equalTo(splitView.mas_bottom).with.inset(20);
    }];
    
    againTextField = [[UITextField alloc] init];
    againTextField.placeholder = @"确认密码";
    againTextField.secureTextEntry = YES;
    [containerView addSubview:againTextField];
    [againTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(againImageView.mas_right).with.offset(20);
        make.right.mas_equalTo(containerView).with.inset(20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(againImageView);
    }];
    
    UIButton *applyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    applyButton.backgroundColor = kWhiteColor;
    applyButton.layer.cornerRadius = 5.0;
    applyButton.layer.masksToBounds = YES;
    [applyButton setTitle:@"确定" forState:UIControlStateNormal];
    [applyButton setTitleColor:kAppRedColor forState:UIControlStateNormal];
    [applyButton addTarget:self action:@selector(setupPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:applyButton];
    [applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(containerView);
        make.top.mas_equalTo(containerView.mas_bottom).with.offset(10);
        make.height.mas_equalTo(44);
    }];
}

- (IBAction)setupPassword:(UIButton *)sender {
    DLog(@"确定登陆密码／重置密码 跳转角色选择");
    
    NSString *password = passwordTextField.text;
    NSString *again = againTextField.text;
    
    
    if (password.length > 1 && again.length > 1) {
        
        if ([password isEqualToString:again]) {
            
            if ([_qh_delegate respondsToSelector:@selector(qh_setupPassword:again:)]) {
                
                [_qh_delegate qh_setupPassword:password again:again];
            }
        } else {
            
            [self showToastWithText:@"两次密码输入不一致，请重新输入"];
        }
    } else {
        
        [self showToastWithText:@"请输入密码"];
    }
}

@end
